package com.dz.lm.common.utils.security;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

/**
 * RSA算法加密/解密工具类。 
 * @author zhanghairong
 * @since 2019-05-28
 */
public class RSAUtil {

	/**
	 * RSA私钥加密
	 * @param key
	 * @param content
	 * @return
	 * @throws CoreException 
	 */
	public static String encryptByRSA(String rsaPriKey, String json){

		try {
			PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(Base64Utils.decode(new String(rsaPriKey.getBytes())));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey privKey = keyFactory.generatePrivate(priv_spec);
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, privKey);
			byte[] data= cipher.doFinal(json.getBytes());
			
			return new String(Base64Utils.encode(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * RSA公钥加密
	 * @param key
	 * @param content
	 * @return
	 * @throws CoreException 
	 */
	public static String encryptByRSAPub(String publicKey, String json) {
		try {
			X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64Utils.decode(new String(publicKey.getBytes())));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyFactory.generatePublic(pubSpec);
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.ENCRYPT_MODE, pubKey);
			byte[] data= cipher.doFinal(json.getBytes());
			return new String(Base64Utils.encode(data));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * RSA私钥解密
	 * @param privateKey
	 * @param content
	 * @return
	 */
	public static byte[] decryptByRSA(String privateKey, String content) {
		byte[] data = null;
		try {
			byte[] keyBytes = Base64Utils.decode(new String(privateKey.getBytes()));  
			PKCS8EncodedKeySpec priv_spec = new PKCS8EncodedKeySpec(keyBytes);
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PrivateKey privKey = keyFactory.generatePrivate(priv_spec);
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.DECRYPT_MODE, privKey);
			data= cipher.doFinal(Base64Utils.decode(new String(content.getBytes())));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
	
	/**
	 * RSA公钥解密
	 * @param publicKey
	 * @param content
	 * @return
	 */
	public static String decryptByRSAPub(String publicKey, String content) {
		byte[] data = null;
		try {
			X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Base64Utils.decode(new String(publicKey.getBytes())));
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			PublicKey pubKey = keyFactory.generatePublic(pubSpec);
			Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cipher.init(Cipher.DECRYPT_MODE, pubKey);
			data= cipher.doFinal(Base64Utils.decode(new String(content.getBytes())));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new String(data);
	}

	/**
	 * 3DES解密
	 * @param key
	 * @param content
	 * @return
	 */
	public static String decryptBy3Des(String key, String content) {
		try {
			byte[] data = Base64Utils.decode(new String(content.getBytes()));
			TrippleDes td = new TrippleDes("PKCS5Padding");
			td.initKey(key.getBytes());
			byte[] de = td.decrypt(data);
			return new String(de);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 3DES加密
	 * 
	 * @param keybyte
	 * @param content
	 * @return
	 */
	public static String encryptBy3Des(String key, String content) {
		try {
			TrippleDes td = new TrippleDes("PKCS5Padding");
			td.initKey(key.getBytes());
			byte[] data = content.getBytes("UTF-8");
			byte[] en = td.encrypt(data);
			return new String(Base64Utils.encode(en));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
}